<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Register extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Register_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    protected $validation_rules = array('registration' => array(array('field' => 'name', 'label' => 'Name', 'rules' => 'trim|required'), array('field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'), array('field' => 'email', 'label' => 'Email', 'rules' => 'required|valid_email|callback_check_email'), array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'), array('field' => 'password2', 'label' => 'Confirm Password', 'rules' => 'trim|required')));

    function index() {
        if (isset($_POST['submit'])) {
            $data = array();
            $data['name'] = $this->input->post('name');
            $data['username'] = $this->input->post('username');
            $data['email'] = $this->input->post('email');
            $data['password'] = $this->input->post('password');
            //$data['password2'] = $this->input->post('password2');
            $data['user_type'] = $this->input->post('user_type');
            $data['create_time'] = date('Y-m-d');
            //print_r($data);

            $this->form_validation->set_rules($this->validation_rules['registration']);
            if ($this->form_validation->run()) {

                 $result = count($this->Register_model->check_user($data['username']));


                if ($result == 0) {
                    $this->Register_model->saveUser($data);
                    $this->session->set_flashdata('msg', 'Registered Successfully.');
                    redirect(base_url());
                } else {
                    $this->session->set_flashdata('msg', 'UserName Already Taken');
                    $this->load->view('register');
                }
            } else {
                $this->load->view('register');
            }
        } else {
            $this->load->view('register');
        }
    }

    function checkUser($name) {
        $result = $this->Register_model->check_user($name);
        echo count($result);
    }

    function check_User() {
        $username = $this->input->post('user');
        $result = $this->Register_model->check_user($username);
        echo count($result);
    }

    function check_email($str)
	{
		$email = $this->Register_model->check_email($str);
		if ($email)
		{
			$this->form_validation->set_message('check_email','Email Already Exist');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

    

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */